﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common.Constants;
using mshtml;
using BotvaBot.BusinessLayer.Enums;
using BotvaBot.BusinessLayer.RequestChainItems.Factory;

namespace BotvaBot.RequestChainItems
{
    [ChainItem]
    public class PlayTakIlTakChainItem : BotvaLogedInChainItemBase
    {
        public int MoneyAmount { get; set; }
        public TakIlTakVariants PlayingVariant { get; set; }

        public bool? IsWin { get; set; }

        protected override void PrepareRequestInternal()
        {
            Url = "http://g3.botva.ru/tavern.php";
            
            Method = HttpMethod.POST;

            AddGet("a", "game");
            AddGet("id", "3");

            string content = Chain.LastContent;
            content = content.Replace(Environment.NewLine, string.Empty);
            while (content.IndexOf("  ") >= 0)
            {
                content = content.Replace("  ", " ");
            }
            content = content.ToUpper();

            IHTMLDocument2 html = new HTMLDocumentClass();
            html.write(content);
            html.close();

            IHTMLElementCollection all = html.body.all as IHTMLElementCollection;

            string kValue = null;

            foreach (IHTMLElement item in all)
            {
                if (item.tagName.ToUpper() != "FORM")
                    continue;

                if (item.getAttribute("action", 0).ToString() == "")
                    continue;

                if (item.getAttribute("action", 0).ToString().Trim().ToUpper() != "?a=game&id=3".ToUpper())
                    continue;

                IHTMLElementCollection inputs = (item.children as IHTMLElementCollection).tags("INPUT") as IHTMLElementCollection;

                bool isFormCorrect = false;
                
                /*
                foreach (IHTMLElement input in inputs)
                {
                    if(!(input is IHTMLInputElement))
                        continue;
                    
                    IHTMLInputElement inp = input as IHTMLInputElement;
                    if (inp.type.Trim().ToUpper() == "SUBMIT" && inp.value.Trim() == "ИГРАТЬ" && input.className.Trim().ToUpper() == "Ccmd_all cmd_row3 cmd_arow3".ToUpper())
                    {
                        isFormCorrect = true;
                    }

                    if (isFormCorrect)
                        break;
                }

                if (!isFormCorrect)
                    continue;
                */

                isFormCorrect = item.outerHTML.Contains("ИГРАТЬ");

                List<IHTMLInputElement> inps = new List<IHTMLInputElement>(inputs.Cast<IHTMLInputElement>());
                isFormCorrect &= inps.Where(i => i.name != null && i.name.Trim() == "K").Count() > 0;
                isFormCorrect &= inps.Where(i => i.name != null && i.name.Trim() == "CMD").Count() > 0;

                if (!isFormCorrect)
                    continue;

                foreach (IHTMLElement input in inputs)
                {
                    IHTMLInputElement inp = input as IHTMLInputElement;

                    if (inp.name == null || inp.name.Trim().Length == 0)
                        continue;

                    if (inp.name.Trim().ToUpper() == "K")
                    {
                        kValue = inp.value.Trim();
                        break;
                    }
                }
            }

            if (kValue != null && kValue.Trim().Length > 0)
            {
                AddPost("k", kValue);
                AddPost("amount", MoneyAmount.ToString());
                AddPost("cmd", "start");
                AddPost("gtype", ((int)PlayingVariant).ToString());
                AddPost("money", "1");          //GOLD
            }
        }

        public override void BeforeDoRequest()
        {
            base.BeforeDoRequest();

            IsWin = null;
        }

        public override void AfterDoRequest()
        {
            base.AfterDoRequest();

            string content = this.Requester.PageContent;
            if (content.ToUpper().Contains("Вы победили и выиграли".ToUpper()))
                IsWin = true;
            if (content.ToUpper().Contains("Вы проиграли и потеряли".ToUpper()))
                IsWin = false;
        }
    }
}

/*
http://g3.botva.ru/castle.php?a=zoo&id=8

block	events
do_cmd	do_big
flying	1333
k	58840

<form method="post" action="">
<input type="hidden" value="58840" name="k">
<input type="hidden" value="events" name="block">
<input type="hidden" value="do_big" name="do_cmd">
<input type="hidden" value="1333" name="flying">
<input class="cmd_all cmd_row4 cmd_arow4 " type="submit" value="ОТПРАВИТЬ">
</form>
*/